﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// 声音管理类
/// </summary>
public class SoundController : MonoBehaviour
{
    private static SoundController m_Ins;

    public static SoundController Instance
    {
        get 
        {
            if (m_Ins == null)
            {
                GameObject go = new GameObject("SoundController");
                m_Ins = go.AddComponent<SoundController>();
                m_Ins.audioSour = go.AddComponent<AudioSource>();
                DontDestroyOnLoad(go);
            }
            return SoundController.m_Ins; 
        }
    }
    private AudioSource audioSour;


    internal void PlaySoundFromResource(string PathAndName, bool PlayOneShot = true, float pitch = 1)
    {
        AudioClip aui = (AudioClip)Resources.Load(PathAndName);
        if (aui != null)
        {
            audioSour.pitch = pitch;
            if (PlayOneShot)
                audioSour.PlayOneShot(aui);
            else
            {
                if (audioSour.isPlaying)
                    audioSour.Stop();
                audioSour.clip = aui;
                audioSour.loop = true;
                audioSour.Play();
            }
        }
        else
            Debug.LogError("path is null:" + PathAndName);
    }

    public void StopLoopSound()
    {
        audioSour.Stop();
    }

}
